<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Promo extends CI_Controller {
	
	private $data;

	function __construct() {
		parent::__construct();
		$this->load->library('Maitaidata');
	}

	public function index()
	{	
		 $data['PromoType'] = $this->getPromoType();	
		 $data['Validity'] = $this->getValidityPeriod();	 
		 $views['_content'] = "choosepromo";		 
		 $this->template->load('template_landing', $views, $data);
	}
	
	public function getPromoTypeList($name = "")
	{
		$name = ($name!="") ? urldecode($name) : "";
		$promo = $this->maitaidata->getPromoTypeList($name);
		$promoObj = $this->cleanUpObj($promo->obj);
		print_r(json_encode($promoObj));
		die();
	}

	public function getPromoValidityList($id = "")
	{
		$id = ($id!="") ? urldecode($id) : "";
		$promo = $this->maitaidata->getPromoValidityList($id);
		$promoObj = $this->cleanUpObj($promo->obj);
		print_r(json_encode($promoObj));
		die();
	}

	public function getPromoList($id = "", $name = "", $validity = "")
	{
		$id = ($id!="") ? urldecode($id) : "";
		$name = ($name!="") ? urldecode($name) : "";
		$validity = ($validity!="") ? urldecode($validity) : "";
		//print($id . " - " . $name . " - " . $validity);
		$promoObj = $this->maitaidata->getPromoList($id, $name, intval($validity));
		$promoObj = $this->cleanUpObj($promoObj->obj);
		print_r(json_encode($promoObj));
		die();
	}

	public function getCommitmentPeriod($id = "", $planprice = "")
	{
		$id = ($id!="") ? urldecode($id) : "";
		$planprice = ($planprice!="") ? intval($planprice) : "";
		//print($id . " - " . $planprice);
		$promoObj = $this->maitaidata->getCommitmentPeriod($id, $planprice);
		$promoObj = $this->cleanUpObj($promoObj->obj);
		print_r(json_encode($promoObj));
		die();
	}
	
	public function registerSubscriber($fName, $mName, $lName, $email, $msisdn, $birthdate, $streetNum, $area, $town, $province, $postalCode, $gadgetId, $promoId, $commitmentPeriod, $storeId, $channelId)
	{
		/*
		//$array = array();
		
		$subscriber = new stdClass();
		$subscriber->fName= "jayar";
		$subscriber->mName= "mendoza";
		$subscriber->lName= "gabriel";
		$subscriber->email= "jr@gmail.com";
		$subscriber->msisdn= "09164046593";
		$subscriber->birthdate= "01/12/1993";
		$subscriber->streetNum= "#303 Palali St.";
		$subscriber->area= "Baranka Itaas";
		$subscriber->town= "Mandaluyong City";
		$subscriber->province= "Metro Manila";
	    $subscriber->postalCode= "1550";
		$subscriber->gadgetId= "10";
		$subscriber->promoId= "22";
		$subscriber->commitmentPeriod= "09/14/2014";
		$subscriber->storeId= "8";
		$subscriber->channelId= "1";
					
		//$array[] = $subscriber;
		//$array[] = $subscriber;
		//$array[] = $subscriber;
		//print_r($array);
		print_r($subscriber); */
		
		
		$promoObj = $this->maitaidata->registerSubscriber($fName, $mName, $lName, $email, $msisdn, $birthdate, $streetNum, $area, $town, $province, $postalCode, $gadgetId, $promoId, $commitmentPeriod, $storeId, $channelId);
		$promoObj = $this->cleanUpObj($promoObj->obj);
		print_r(json_encode($promoObj));
		die();
	}


	private function getPromoType() {
		$data = array( 
			"1"=>array("promoID"=> "1","promoName"=> "ALLNET","promoDesc"=> "With UNLIMITED texts to all networks plus minutes of calls to Globe/ TM"),
			"2"=>array("promoID"=> "2","promoName"=> "UNLI","promoDesc"=> "With UNLIMITED calls to Globe/ TM plus texts to all networks"),
		);	
		return $data; 
	}
	
	private function getValidityPeriod(){
		$data = array( 
			array("promoID"=> "1","promoName"=> "ALLNET","validity"=> "1 Day"),
			array("promoID"=> "2","promoName"=> "ALLNET","validity"=> "15 Days"),
			array("promoID"=> "3","promoName"=> "ALLNET","validity"=> "30 Days"),
		);	
		//var_dump($data); 
		return $data; 
	}
	
	public function getGadgetId($gadgetid=""){
		$this->load->model('products');
		$gadget_id = $this->products->get_gadgetid($gadgetid);
		print_r(json_encode($gadget_id));
		die();
	}

	private function getPromo($type){
		// or put this in model
	}

	private function getCommitRebate($promo , $validity){
		// or put this in model

	}

	
	
	private function cleanUpObj($promoObj)
	{
		foreach($promoObj as $item)
		{
			unset($item->__type);
		}
		return $promoObj;
	}	
	
	/* . . . . . . . . . . . promo hessian service calls . . . . . . . . . . . */
	public function serviceCalls(){
		if(isset($_POST['isAjax'])){
			/*  ajax frontend -> controller    */

			// prep available params
			// determine the service call type from library
			// prepare post data 

			$params = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter 
			echo json_encode( $params["promoTypeId"] ); die(); /* . . . . practice  . . . */
			// var_dump($params); die(); // i try muna ang xss filter
			// $response = $this->maitaidata->getServices($params['service'],$params['promoTypeId'],$params['promoTypeName'],$params['validity'],$params['promoId'],$params['gadgetPlanPrice']);
			// echo json_encode($response);
			return true;

		}	
		echo "Access Denied ";


	}

}
